export class Controller {
    static instance: Controller = new Controller();
    getRandomColor() {
        return Math.floor(Math.random() * 5)
    }
    
    getColorIdx (color1: number, color2: number, x: number, y: number): number {
        if (color1 > 4 || color2 > 4) return this.getRandomColor();
        if (color1 === -1 && color2 === -1) return this.getRandomColor();
        x = Math.max(-20, (Math.min(x, 30)));
        y = Math.max(-20, (Math.min(y, 80)));
        let weight_arr = new Array(5);
        // 基础权重20
        let base_weight = 20;
        for (let i = 0; i < weight_arr.length; i++) {
            if (color1 !== color2) {
                if (i === color1 || i === color2) {
                    weight_arr[i] = base_weight + x;
                } else {
                    weight_arr[i] = (60 - 2 * x) / 3;
                }
            } else {
                if (i === color1) {
                    weight_arr[i] = base_weight + y;
                } else {
                    weight_arr[i] = (80 - y) / 4;
                }
            }            
        }
        console.log(weight_arr);
        let random = Math.random() * 100;
        console.log(random)
        let weight_sum = 0;
        for (let i = 0; i < weight_arr.length; i ++) {
            weight_sum += weight_arr[i];
            if (weight_sum > random) {
                console.log("i: ", i)
                return i
            }
        }
        return this.getRandomColor();
    }
}
export const controller = Controller.instance;

// console.log(controller.getRandomColor());
// controller.getColorIdx(2, 2, 10, 20);